﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using yafc.Data;

namespace yafc.UI.Components
{
	public partial class PostItem : ListBoxItem
	{
		private Post post;

		public event EventHandler<EventArgs> Reply;
		public event EventHandler<EventArgs> Retweet;

		public Post Post
		{
			get { return post; }
			set { post = value; Invalidate(); }
		}

		protected Font NameFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("NameFont");
			}
		}
		protected Font TimeFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("TimeFont");
			}
		}
		protected Font TextFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("TextFont");
			}
		}
		protected Font FloatButtonFont
		{
			get {
				return ThemeManager.GetCurrentTheme().GetFont("FloatButtonFont");
			}
		}

		protected Brush NameBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("NameBrush");
			}
		}
		protected Brush TimeBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("TimeBrush");
			}
		}
		protected Brush TextBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("TextBrush");
			}
		}
		protected Brush BgBrush
		{
			get
			{
				if (!Selected)
				{
					return ThemeManager.GetCurrentTheme().GetBrush("BgBrush");
				}
				else
				{
					return ThemeManager.GetCurrentTheme().GetBrush("HighlightBgBrush");
				}
			}
		}
		protected Brush SeparateBrush
		{
			get { return TextBrush; }
		}

		protected Pen SeparatePen
		{
			get
			{
				return new Pen(SeparateBrush);
			}
		}

		protected int ButtonX
		{
			get { return ClientSize.Width - Padding.Right - replyBtn.Width; }
		}
		protected Padding ButtonMargin
		{
			get { return replyBtn.Margin; }
		}

		public PostItem(Post p)
		{
			InitializeComponent();

			SuspendLayout();

			Post = p;
			nameLabel.Text = Post.post.name;
			timeLabel.Text = Post.post.timestamp.ToString();
			textField.Text = Post.post.text;

			avatar.LoadAsync(Post.post.avatar);

			ResumeLayout(false);
		}

		private void retweet_Click(object sender, EventArgs e)
		{
			Select(true);
			OnRetweet(EventArgs.Empty);
		}
		private void reply_Click(object sender, EventArgs e)
		{
			Select(true);
			OnReply(EventArgs.Empty);
		}

		protected void OnReply(EventArgs e)
		{
			var temp = Interlocked.CompareExchange(ref Reply, null, null);
			if (temp != null)
				temp(this, e);
		}
		protected void OnRetweet(EventArgs e)
		{
			var temp = Interlocked.CompareExchange(ref Retweet, null, null);
			if (temp != null)
				temp(this, e);
		}

		private void PostItem_Paint(object sender, PaintEventArgs e)
		{
			var g = e.Graphics;
			var origBounds = e.ClipRectangle;

			var bounds = new Rectangle();
			bounds.Location = new Point(0, 0);
			bounds.Size = Size;

			DrawBg(g, bounds);

			g.DrawLine(SeparatePen, new Point(0, bounds.Size.Height - (int)SeparatePen.Width), new Point(bounds.Size.Width, bounds.Size.Height - (int)SeparatePen.Width));
		}
		protected virtual void DrawBg(Graphics g, Rectangle bounds)
		{
			g.FillRectangle(BgBrush, bounds);
		}
		protected virtual void DrawSelectedRectangle(Graphics g, Rectangle bounds)
		{
			int x1 = bounds.X;
			int y1 = bounds.Y;
			int x2 = bounds.X + bounds.Width;
			int y2 = bounds.Y + bounds.Height;

			int minEdge = Padding.Left;

			using (var pen = new Pen(BgBrush, minEdge))
			{
				g.DrawLine(pen, new Point(x1, y1 + minEdge / 2), new Point(x2, y1 + minEdge / 2));

				g.DrawLine(pen, new Point(x1, y2 - minEdge / 2), new Point(x2, y2 - minEdge / 2));

				g.DrawLine(pen, new Point(x1 + minEdge / 2, y1), new Point(x1 + minEdge / 2, y2));

				g.DrawLine(pen, new Point(x2 - minEdge / 2, y1), new Point(x2 - minEdge / 2, y2));
			}
		}

		private void PostItem_Layout(object sender, LayoutEventArgs e)
		{
			if (Parent == null)
				return;

			Width = Parent.ClientSize.Width;
			textField.Width = ButtonX - textField.Location.X - ButtonMargin.Left;
			Height = Math.Max(textField.Location.Y + textField.Height + Padding.Bottom,
				avatar.Location.Y + avatar.Height + Padding.Bottom);
			Invalidate();
		}
		private void DefaultMouseHover(object sender, EventArgs e)
		{
			Parent.Focus();
		}

		private void nameLabel_Click(object sender, EventArgs e)
		{
			OnReply(EventArgs.Empty);
		}

		private void DefaultMouseEnter(object sender, EventArgs e)
		{
			Parent.Focus();
		}

		private void retweetBtn_Click(object sender, EventArgs e)
		{
			OnRetweet(EventArgs.Empty);
		}

		private void replyBtn_Click(object sender, EventArgs e)
		{
			OnReply(EventArgs.Empty);
		}

		private void DefaultMouseClick(object sender, MouseEventArgs e)
		{
			this.OnMouseClick(e);
		}

		private void DefaultMouseMove(object sender, MouseEventArgs e)
		{
			Parent.Focus();
		}
	}
}
